package com.zhu.pre.service.impl;

import com.sanploy.common.domain.User;
import com.sanploy.common.domain.UserAddress;
import com.sanploy.common.domain.vo.UserAddressVo;
import com.sanploy.common.enums.Meta;
import com.sanploy.common.util.CommonUtil;
import com.sanploy.common.vo.BaseException;

import com.zhu.pre.mapper.MUserAddressMapper;
import com.zhu.pre.service.MUserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * Created by yingzhi zhu on 2017/9/26.
 * @author yingzhi zhu
 * 手机用户收货地址逻辑处理接口实现
 */
@Service
public class MUserAddressServiceImpl implements MUserAddressService {

    @Autowired
    private MUserAddressMapper addressMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public int addUserAddress(UserAddress userAddress,String token) throws BaseException {
        userAddress.setUser_id(Optional.ofNullable((User)redisTemplate.opsForValue().get(token)).
                orElseThrow(()->new BaseException(Meta.AuthFail)).getId());
        return CommonUtil.judgeRUD(addressMapper.addUserAddress(userAddress),Meta.InsertFail);
    }


    @Override
    public UserAddressVo getUserDefaultAddress(String token) throws BaseException {
        return addressMapper.selectUserDefaultAddress(Optional.ofNullable((User)redisTemplate.opsForValue().get(token)).
                orElseThrow(()->new BaseException(Meta.AuthFail)).getId());
    }

    @Override
    public int setDefaultAddress(String token, Long id) throws BaseException {
        return CommonUtil.judgeRUD(addressMapper.updateDefaultAddress
                (id,addressMapper.selectUserDefaultAddress(
                        Optional.ofNullable((User)redisTemplate.opsForValue().get(token)).
                                orElseThrow(()->new BaseException(Meta.AuthFail)).getId()).getOrder_sort()+1)
                ,Meta.UpdateFail);
    }

    @Override
    public int deleteAddress(Long id, String token) throws BaseException {
        return addressMapper.deleteAddress(id,Optional.ofNullable((User)redisTemplate.opsForValue().get(token)).
                orElseThrow(()->new BaseException(Meta.AuthFail)).getId());
    }


    @Override
    public int updateAddress(UserAddress userAddress,String token) throws BaseException {
        userAddress.setUser_id( Optional.ofNullable((User)redisTemplate.opsForValue().get(token)).
                orElseThrow(()->new BaseException(Meta.AuthFail)).getId());
        return addressMapper.updateAddress(userAddress);
    }

    @Override
    public UserAddressVo findOneAddress(Long id,String token) throws BaseException {
        return addressMapper.selectOneAddress(id,
                Optional.ofNullable((User)redisTemplate.opsForValue().get(token)).
                orElseThrow(()->new BaseException(Meta.AuthFail)).getId()
        );
    }

    @Override
    public List<UserAddressVo> getUserAddress(String token) throws BaseException {
        return addressMapper.selectUserAddress(Optional.ofNullable((User)redisTemplate.opsForValue().get(token)).
                orElseThrow(()->new BaseException(Meta.AuthFail)).getId());
    }


}
